package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.entity.VideoParseRecords;

import java.util.List;

@Mapper
public interface VideoParseRecordMapper extends BaseMapper<VideoParseRecords> {
    // 在 VideoParseRecordMapper.java 中添加
    @Select("SELECT DISTINCT platform FROM video_parse_records WHERE platform IS NOT NULL AND platform != ''")
    List<String> getAllPlatforms();

    // 获取所有不同的昵称
    @Select("SELECT DISTINCT nickname FROM video_parse_records WHERE nickname IS NOT NULL AND nickname != ''")
    List<String> getAllNicknames();

    // 查看解析记录（带分页和模糊查询）- 只查询指定字段
    @Select("<script>" +
            "SELECT nickname, original_url, platform, parse_time " +
            "FROM video_parse_records " +
            "<where>" +
            "   <if test='nickname != null'>" +
            "       AND nickname LIKE CONCAT('%', #{nickname}, '%')" +
            "   </if>" +
            "   <if test='platform != null'>" +
            "       AND platform LIKE CONCAT('%', #{platform}, '%')" +
            "   </if>" +
            "   <if test='keyword != null'>" +
            "       AND (CONVERT(original_url USING utf8) LIKE CONCAT('%', #{keyword}, '%') " +
            "           OR CONVERT(parsed_video_url USING utf8) LIKE CONCAT('%', #{keyword}, '%'))" +
            "   </if>" +
            "</where>" +
            "ORDER BY parse_time DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<VideoParseRecords> getVideoParseRecords(@Param("nickname") String nickname,
                                                 @Param("platform") String platform,
                                                 @Param("keyword") String keyword,
                                                 @Param("offset") int offset,
                                                 @Param("pageSize") int pageSize);

    // 获取总记录数（用于分页）
    @Select("<script>" +
            "select count(*) from video_parse_records " +
            "<where>" +
            "   <if test='nickname != null'>" +
            "       and nickname like concat('%', #{nickname}, '%')" +
            "   </if>" +
            "   <if test='platform != null'>" +
            "       and platform like concat('%', #{platform}, '%')" +
            "   </if>" +
            "   <if test='keyword != null'>" +
            "       and (original_url like concat('%', #{keyword}, '%') " +
            "           or parsed_video_url like concat('%', #{keyword}, '%'))" +
            "   </if>" +
            "</where>" +
            "</script>")
    int countVideoParseRecords(@Param("nickname") String nickname,
                               @Param("platform") String platform,
                               @Param("keyword") String keyword);



} 